package com.lixiaoyao.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lixiaoyao.reggie.entity.Category;
import com.lixiaoyao.reggie.entity.R;
import com.lixiaoyao.reggie.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/category")
@Slf4j
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @PostMapping
    public R<String> sava(@RequestBody Category category){

        log.info(category.toString());
        log.info(categoryService.toString());
        categoryService.save(category);

        return R.success("添加成功");
    }
    @GetMapping("/page")
    public R<Page> getPage(int page,int pageSize){

        Page<Category> categoryPage = new Page<>(page, pageSize);
        categoryService.page(categoryPage);
        return R.success(categoryPage);
    }
    @DeleteMapping
    public R<String> delete(Long ids){
        categoryService.remove(ids);

        return R.success("删除成功");
    }
    @PutMapping
    public R<String> update(@RequestBody Category category){

        categoryService.updateById(category);
        return R.success("修改分类成功");
    }
    @RequestMapping("/list")
    public R<List<Category>> list(Category category){

        LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper<Category>();
        lambdaQueryWrapper.eq(category.getType()!=null,Category::getType,category.getType());
        lambdaQueryWrapper.orderByAsc(Category::getSort);
        List<Category> list = categoryService.list(lambdaQueryWrapper);
        return R.success(list);
    }
}
